package com.example.dao;

import com.example.bean.Student;
import com.example.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StudentSearchDaoImp implements StudentSearchDao{

    @Override
    public Student getStudentSearch(Student student) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try {
            //1.获取连接
            connection= DBUtil.getConnection();
            //2.编写SQL语句
            String sql="select * from s_user where sname=?";
            //3.预处理对象
            preparedStatement = connection.prepareStatement(sql);
            //4.设置占位符
            preparedStatement.setString(1,student.getSname());
            //5.处理结果集
            ResultSet resultSet = preparedStatement.executeQuery();
            while(resultSet.next()){
                String sname = resultSet.getString("sname");
                String ssex = resultSet.getString("ssex");
                String sage = resultSet.getString("sage");
                String semail = resultSet.getString("semail");
                String stext = resultSet.getString("stext");
                Student tu=new Student(sname,ssex,sage,semail,stext);
                return tu;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //6.释放相关的资源
            try {
                DBUtil.closeResource(connection,preparedStatement);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
